package com.uchedao.vehicledetection.activity;

import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.uchedao.mybaselibrary.utils.LogUtil;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.adapter.MyCarSourceAdapter;
import com.uchedao.vehicledetection.config.Api;
import com.uchedao.vehicledetection.model.MyCarResouceEntity;
import com.uchedao.vehicledetection.model.response.MyCarListResponse;
import com.uchedao.vehicledetection.network.HttpRequest;
import com.uchedao.vehicledetection.utils.ProgressDialogUtil;
import com.uchedao.vehicledetection.view.widget.pulltorefresh.library.PullToRefreshBase;
import com.uchedao.vehicledetection.view.widget.pulltorefresh.library.PullToRefreshListView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的车源
 */
public class MyCarSourceActivity extends CJDBaseActivity {

    private TextView tvNoData;
    private PullToRefreshListView lvSource;

    private int pageNo;
    private int pageSize;
    private MyCarSourceAdapter adapter;

    @Override
    protected String getActivityTag() {
        return "MyCarSourceActivity";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_my_car_source;
    }

    @Override
    protected void initView() {
        tvNoData = (TextView) findViewById(R.id.tvNoData);
        lvSource = (PullToRefreshListView) findViewById(R.id.lvMyCarSource);
    }

    @Override
    protected void initData() {
        setTitle("我的车源");

        lvSource.setMode(PullToRefreshBase.Mode.BOTH);
        pageNo = 0;
        pageSize = 15;
        ProgressDialogUtil.showProgressDlg(this, "数据加载中...");
        getSourceList(pageNo, pageSize);

    }

    @Override
    protected void initListener() {
        lvSource.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                pageNo = 0;
                getSourceList(pageNo, pageSize);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                getSourceList(pageNo, pageSize);
            }
        });
    }

    /**
     * 获取车源列表
     */
    public void getSourceList(int pageNo, int pageSize) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("offset", pageNo*pageSize+"");
        params.put("limit", pageSize+"");
        Request req = HttpRequest.getRequest(HttpRequest.GET, Api.Action.MY_CAR, params,
                new Response.Listener<JsonObject>() {
                    @Override
                    public void onResponse(JsonObject response) {
                        lvSource.onRefreshComplete();
                        ProgressDialogUtil.dismissProgressDlg();
                        MyCarListResponse entity = new Gson().fromJson(response, MyCarListResponse.class);
                        updateView(entity.getList());
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        lvSource.onRefreshComplete();
                        ProgressDialogUtil.dismissProgressDlg();
                        LogUtil.e(error.getMessage());
                    }
                });
        addQueue(req);
    }

    public void updateView(List<MyCarResouceEntity> list) {

        if(pageNo==0 || adapter==null) {
            adapter = new MyCarSourceAdapter(this, list);
            lvSource.setAdapter(adapter);
        }
        if(pageNo > 0) {
            adapter.getmData().addAll(list);
            adapter.notifyDataSetChanged();
        }

        if(adapter.getmData()!=null && adapter.getmData().size()>0) {
            tvNoData.setVisibility(View.GONE);
        }else{
            tvNoData.setVisibility(View.VISIBLE);
        }
        pageNo++;
    }
}
